package jianzhiOffer;

import java.util.Stack;

//剑指 Offer 31. 栈的压入、弹出序列
public class Num31_validateStackSequences {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        //借助辅助栈实现
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        for (int num : pushed){
            //入栈
            stack.push(num);
            //栈不为空，并且栈顶元素和弹出元素相等
            while (!stack.isEmpty() && stack.peek() == popped[i]){
                stack.pop();
                i++;
            }
        }
        return stack.isEmpty();
    }

}
